import time

from selenium import webdriver
from selenium.common import WebDriverException, NoSuchElementException, InvalidArgumentException, TimeoutException
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select

from common.functions import open_browser


class Base:
    # 打开浏览器
    def __init__(self, browser):
        self.driver = browser

    # 请求目标网页
    def get(self, url):
        try:
            self.driver.get(url)
        except (WebDriverException, InvalidArgumentException):
            print("输入的网址不正确")

    # 强制等待
    def time(self,seconds=3):
        time.sleep(seconds)

    # 定义最大化浏览器窗口方法
    def window(self):
        self.driver.maximize_window()

    # 元素定位
    def find_element(self, locator,timeout=5):  # locator 元组
        try:
            return WebDriverWait(self.driver,timeout).until(EC.presence_of_element_located(locator))
        except TimeoutException:
            print("元素定位失败")

    # 切换进iframe
    def into_frame(self, locator):
        #进入frame
        self.driver.switch_to.frame(self.find_element(locator))

    def back_default(self):
        #返回主页面
        self.driver.switch_to.default_content()

    def quit_frame(self):
        #退出所有iframe
        self.driver.switch_to.parent_frame()

    # 下拉框(value值)
    def Select(self,locator,value):
        try:
            element = self.find_element(locator=locator)  # 定位选择框select的元素
            sel = Select(element)  # 实例化对象
            sel.select_by_value(value)  # 通过value值查找定位的元素
        except:
            print(f'元素{locator}不存在,无法输入')

    # 下拉框（下标）
    def select_by_index(self,locator, index):
        ele = self.find_element(locator)
        select = Select(ele)
        select.select_by_index(index)

    # 点击
    def click(self, locator):
        element = self.find_element(locator)
        if element is not None:
            element.click()
        else:
            print("元素定位失败")

    # 定义输入内容方法
    def send_keys(self,locator,text):
        element = self.find_element(locator)
        if element is not None:
            element.send_keys(text)

    def get_attribute(self, locator, attr):
        # 获取元素属性
        element = self.find_element(locator)
        if element:
            return element.get_attribute(attr)


    def assertEqual(self,a,b):
        self.assertEqual(a,b)


    def get_element_text(self, locator):
        # 获取元素文本
        element = self.find_element(locator)
        if element:
            return element.text

    # 清除内容再填入
    def keys(self,element,price):
        shop = self.find_element((element))
        shop.send_keys(Keys.CONTROL,'a')
        shop.send_keys(Keys.CONTROL,'x')
        shop.send_keys(price)

    # 定义浏览器后退方法
    def back(self):
        self.driver.back()

    # 定义浏览器前进方法
    def forward(self):
        self.driver.forward()

    # 定义浏览器刷新方法
    def refresh(self):
        self.driver.refresh()

    # 退出浏览器
    def quit(self, seconds=3):
        time.sleep(seconds)
        self.driver.quit()

    #  隐式等待
    def implicitly_wait(self, second=10):
        self.driver.implicitly_wait(second)

    #  清空操作
    def clear(self, locator):
        # 清空文本
        element = self.find_element(locator)
        if element:
            element.clear()

    #  获取弹窗文本信息
    def get_alert_text(self):
        alert = self.driver.switch_to.alert
        text = alert.text
        alert.accept()
        return text

    # 弹窗处理
    def alert_operation(self):
        time.sleep(2)
        self.driver.switch_to.alert.accept()





if __name__ == '__main__':
    browser = open_browser()
    base = Base(browser)
    base.get("https://www.baidu.com")
    kw = base.find_element((By.ID, "kw"),3)
    print(kw.get_attribute("outerHTML"))
    base.quit()
